package com.cjkj.system.controller;

import com.cjkj.common.core.web.controller.BaseController;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.common.log.annotation.Log;
import com.cjkj.common.log.enums.BusinessType;
import com.cjkj.common.security.annotation.RequiresPermissions;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.THospitalDept;
import com.cjkj.system.domain.dto.HospitalDeptReq;
import com.cjkj.system.domain.vo.HospitalDeptVo;
import com.cjkj.system.service.ITHospitalDeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 科室信息
 */
@RestController
@RequestMapping("/dept")
public class HospitalDeptController extends BaseController
{
    @Resource
    private ITHospitalDeptService deptService;

    @GetMapping("/deptTree")
    public AjaxResult deptTree()
    {
        String hospitalId = SecurityUtils.getLoginUser().getSysUser().getHospitalId();
        return AjaxResult.success(deptService.selectDeptTreeList(hospitalId));
    }

    /**
     * 获取科室列表
     */
    @RequiresPermissions("system:dept:list")
    @GetMapping("/list")
    public AjaxResult list(HospitalDeptReq req)
    {
        req.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());

        List<HospitalDeptVo> list = deptService.selectTHospitalDeptList(req);
        return AjaxResult.success(list);
    }

    /**
     * 新增科室
     */
    @RequiresPermissions("system:dept:add")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody THospitalDept dept)
    {
        dept.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());
        return toAjax(deptService.insertTHospitalDept(dept));
    }

    /**
     * 删除科室
     */
    @RequiresPermissions("system:dept:remove")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable String deptId)
    {
        return toAjax(deptService.deleteTHospitalDeptByDeptId(deptId));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("system:dept:query")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable String deptId)
    {
        return success(deptService.selectTHospitalDeptByDeptId(deptId));
    }

    /**
     * 修改科室
     */
    @RequiresPermissions("system:dept:edit")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody THospitalDept dept) {
        dept.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());
        return toAjax(deptService.updateTHospitalDept(dept));
    }

}
